<?php
require_once "database.php";
require_once "注册.html";
$database = mysql_link();
$conn=mysqli_connect(HOSTNAME,
    USERNAME,
    PASSWORK,
    DATABASE,
    PORT);

$password = $_POST['password'];
$email = $_POST['email'];
$password_y = $_POST['password_y'];




if (isset($_POST['submit'])) {
    if (empty($_POST['email'])) {
        echo "<script>alert('email不能为空')</script>";
        die();
    } elseif (empty($_POST['password'])){
        echo "<script>alert('密码不能为空')</script>";
        die();
    } elseif (empty($_POST['password_y'])) {
        echo "<script>alert('确认密码')</script>";
        die();
    } else {
        $sql_sel = "select * from users where email = '$email'";
        $sel = mysqli_query($conn,$sql_sel);
        $num = mysqli_num_rows($sel);

        if ($num) {
            echo "<script>alert('已存在')</script>";
            die();
        } else {
            if ($password === $password_y){
                $sql1 = "insert into users(Name,Password,Email,Role) values('用户名','$password','$email',2)";
                $res = mysqli_query($conn, $sql1);

                if ($res){
                    echo "<script>alert('成功注册');location.href='login.html'</script>";

                }else{

                    echo "<script>alert('注册失败')</script>";

                }
            }else{
                echo "<script>alert('两次密码不符')</script>";
                die();
            }

        }
    }
}